<template>
  <div
    class="relative h-screen bg-white dark:zinc-800 text-center xl:bg-zinc-200"
  >
    <!-- 头部图片 -->
    <headerVue></headerVue>
    <!-- 表单区 -->
    <div
      class="block px-3 mt-4 dark:bg-zinc-800 xl:bg-white xl:w-[380px] xl:dark:bg-zinc-900 xl:m-auto xl:mt-8 xl:py-4 xl:rounded-sm xl:shadow-lg"
    >
      <h3
        class="hidden mb-2 font-semibold text-base text-main dark:text-zinc-300 xl:block"
      >
        账号登陆
      </h3>

      <!-- 表单 -->
      <vee-form @submit="onLoginHandler">
        <!-- 用户名 -->
        <vee-field
          class="dark:bg-zinc-800 dark:text-zinc-400 border-b-zinc-400 border-b w-full outline-0 pb-1 px-1 text-base focus:border-b-main dark:focus:border-b-zinc-200 xl:default:bg-zinc-900"
          name="username"
          placeholder="用户名"
          type="text"
          :rules="validateUsername"
          v-model="loginForm.username"
        />
        <vee-error-message
          class="text-sm text-red-600 block mt-0.5 text-left"
          name="username"
        ></vee-error-message>
        <!-- 密码 -->
        <vee-field
          class="dark:bg-zinc-800 dark:text-zinc-400 border-b-zinc-400 border-b w-full outline-0 pb-1 px-1 text-base focus:border-b-main dark:focus:border-b-zinc-200 xl:default:bg-zinc-900"
          type="password"
          name="password"
          placeholder="密码"
          :rules="validatePassword"
          v-model="loginForm.password"
        />
        <vee-error-message
          class="text-sm text-red-600 block mt-0.5 text-left"
          name="password"
        ></vee-error-message>
        <!-- 跳转按钮 -->
        <div class="pt-1 pb-3 leading-[0px] text-right">
          <a
            @click="onToRegister"
            class="inline-block p-1 text-zinc-400 text-right dark:text-zinc-600 hover:text-zinc-600 dark:hover:text-zinc-400 text-sm duration-300 cursor-pointer"
            >去注册</a
          >
        </div>

        <!-- 登陆按钮 -->
        <m-button
          class="w-full dark:bg-zinc-900 xl:dark:bg-zinc-800"
          :loading="loading"
          :isActiveAnim="false"
          >登陆</m-button
        >
        <!-- 第三方登陆 -->
        <div class="flex justify-around mt-4">
          <!-- QQ -->
          <m-svg-icon class="w-4 cursor-pointer" name="qq"></m-svg-icon>
          <!-- 微信 -->
          <m-svg-icon class="w-4 cursor-pointer" name="wexin"></m-svg-icon>
        </div>
      </vee-form>
    </div>
    <SliderCaptchaVue
      v-if="isSliderCaptchaVisible"
      @close="isSliderCaptchaVisible = false"
      @success="onCaptchaSuccess"
    ></SliderCaptchaVue>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import headerVue from '../components/header.vue'
import {
  Form as veeForm,
  Field as veeField,
  ErrorMessage as veeErrorMessage
} from 'vee-validate'
import { validateUsername, validatePassword } from '../validate.js'
import SliderCaptchaVue from './slider-captcha.vue'
import { LOGIN_TYPE_USERNAME } from '@/constants/index.js'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'

const store = useStore()
const router = useRouter()

// 控制 sliderCaptcha 展示
const isSliderCaptchaVisible = ref(false)

/**
 * 触发登陆 表单校验通关以后触发  PS:button 在 vee-form 之内，默认就会触发 submit 事件
 */
const onLoginHandler = () => {
  isSliderCaptchaVisible.value = true
}

/**
 * 人类行为验证通过
 */

const onCaptchaSuccess = () => {
  isSliderCaptchaVisible.value = false
  // 登陆操作
  onLogin()
}

/**
 * 用户登陆行为
 */
const loading = ref(false)
const loginForm = ref({
  username: '',
  password: ''
})
const onLogin = async () => {
  loading.value = true
  // 执行登陆操作
  try {
    await store.dispatch('user/login', {
      ...loginForm.value,
      loginType: LOGIN_TYPE_USERNAME
    })
  } finally {
    loading.value = false
  }
  router.push('/')
}

/**
 * 去注册
 */
const onToRegister = () => {
  store.commit('app/changeRouterType', 'push')
  router.push('/register')
}
</script>

<style lang="scss" scoped></style>
